package chat;

//================== Message.java ==================
import java.io.Serializable;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

class Message implements Serializable {
    public enum Type {
        LOGIN, LOGOUT, GROUP_CHAT, PRIVATE_CHAT, USER_LIST_UPDATE, 
        FILE_TRANSFER, GROUP_FILE, HISTORY_REQUEST, KICK_USER,
        GROUP_MEMBER_LIST, GROUP_MEMBER_REQUEST,IP_UPDATE,
        SUBGROUP_INVITE,   // 小组邀请
        SUBGROUP_ACCEPT,   // 接受小组邀请
        SUBGROUP_REJECT,   // 拒绝小组邀请
        SUBGROUP_CHAT,     // 小组聊天
        SUBGROUP_LEAVE,    // 离开小组
        IMAGE,             // 图片消息
        IMAGE_DOWNLOAD_REQUEST, // 新增：图片下载请求
        IMAGE_DOWNLOAD_RESPONSE // 新增：图片下载响应
    }

    private Type type;
    private String sender;
    private String receiver;
    private String content;
    private String timestamp;
    private byte[] fileData;
    private String fileName;
    private String group;
    private String subgroupId; // 小组ID
    private List<String> memberList; // 成员列表
    private Boolean voiceRegistered;
    private String ipAddress;
    private int voicePort=-1; // 新增语音端口字段

    public Message(Type type, String sender, String content) {
        this.type = type;
        this.sender = sender;
        this.content = content;
        this.timestamp = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }

    public void setTimestamp(String timestamp) {
        this.timestamp = timestamp;
    }

    // Getters and Setters
    public Type getType() { return type; }
    public void setType(Type type) { this.type = type; }

    public String getSender() { return sender; }
    public void setSender(String sender) { this.sender = sender; }

    public String getReceiver() { return receiver; }
    public void setReceiver(String receiver) { this.receiver = receiver; }

    public String getContent() { return content; }
    public void setContent(String content) { this.content = content; }

    public String getTimestamp() { return timestamp; }


    public byte[] getFileData() { return fileData; }
    public void setFileData(byte[] fileData) { this.fileData = fileData; }

    public String getFileName() { return fileName; }
    public void setFileName(String fileName) { this.fileName = fileName; }

    public String getGroup() { return group; }
    public void setGroup(String group) { this.group = group; }
    
    public String getSubgroupId() { return subgroupId; }
    public void setSubgroupId(String subgroupId) { this.subgroupId = subgroupId; }
    
    public List<String> getMemberList() { return memberList; }
    public void setMemberList(List<String> memberList) { this.memberList = memberList; }

    public Boolean getVoiceRegistered() { return voiceRegistered; }
    public void setVoiceRegistered(Boolean voiceRegistered) { this.voiceRegistered = voiceRegistered; }

    public String getIpAddress() { return ipAddress; }
    public void setIpAddress(String ipAddress) { this.ipAddress = ipAddress; }

    public int getVoicePort() { return voicePort; } // 新增getter
    public void setVoicePort(int port) {
        // 验证端口有效性
        if (port <= 0 || port > 65535) {
            throw new IllegalArgumentException("无效的端口号: " + port);
        }
        this.voicePort = port;
    }
}